package it.blueocean.acanto.security.userdetails;

import it.blueocean.acanto.entities.platform.security.Authority;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

public class UtenteBean implements UserDetails, Serializable
{

	private static final long serialVersionUID = 8510709537282350943L;

	private String username;

	private String password;

	private Boolean enabled;

	private Boolean accountNonExpired;

	private Boolean accountNonLocked;

	private Boolean credentialsNonExpired;

	private Set<Authority> roles = new HashSet<Authority>();

	public UtenteBean(String principal)
	{
		this.username = principal;
	}

	public boolean equals(Object obj)
	{
		if(obj instanceof UtenteBean)
		{
			return ((UtenteBean) obj).getUsername().equalsIgnoreCase(this.getUsername());
		}

		return false;
	}

	public int hashCode()
	{
		final int numeroPrimo = 31;
		int result = 1;

		result = numeroPrimo * result + ((this.getUsername() == null) ? 0 : this.getUsername().hashCode());

		return enabled.hashCode();
	}

	@Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("UtenteBean [username=");
		builder.append(username);
		builder.append(", enabled=");
		builder.append(enabled);
		builder.append(", accountNonExpired=");
		builder.append(accountNonExpired);
		builder.append(", accountNonLocked=");
		builder.append(accountNonLocked);
		builder.append(", credentialsNonExpired=");
		builder.append(credentialsNonExpired);
		builder.append(", roles=");
		builder.append(roles);
		builder.append("]");
		return builder.toString();
	}

	public Set<Authority> getRoles()
	{
		return roles;
	}

	public void setRoles(Set<Authority> roles)
	{
		this.roles = roles;
	}

	@Override
	public Collection<GrantedAuthority> getAuthorities()
	{
		List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();

		for (Iterator<Authority> roleIterator = roles.iterator(); roleIterator.hasNext();)
		{
			Authority role = roleIterator.next();
			GrantedAuthority authority = role.getAuthority();
			list.add(authority);
		}

		return list;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public void setEnabled(Boolean enabled)
	{
		this.enabled = enabled;
	}

	public void setAccountNonExpired(Boolean accountNonExpired)
	{
		this.accountNonExpired = accountNonExpired;
	}

	public void setAccountNonLocked(Boolean accountNonLocked)
	{
		this.accountNonLocked = accountNonLocked;
	}

	public void setCredentialsNonExpired(Boolean credentialsNonExpired)
	{
		this.credentialsNonExpired = credentialsNonExpired;
	}

	@Override
	public String getPassword()
	{
		return this.password;
	}

	@Override
	public String getUsername()
	{
		return this.username;
	}

	@Override
	public boolean isAccountNonExpired()
	{
		return this.accountNonExpired;
	}

	@Override
	public boolean isAccountNonLocked()
	{
		return this.accountNonLocked;
	}

	@Override
	public boolean isCredentialsNonExpired()
	{
		return credentialsNonExpired;
	}

	@Override
	public boolean isEnabled()
	{
		return this.enabled;
	}

}
